"""
所有窗口控件类都需要继承 baseComponent
新增窗体控件，需要添加到 /config/ComponentList.json 里面
添加了会找得更快，没有添加的，只要名字没错，也能找到
"""
import copy
from TestToolsV2.data.ReadConfig import readConfig
from TestToolsV2.tools.Log import Log
from TestToolsV2.view.OpenDialog import openDialog

log = Log()


class baseComponent:
    def __init__(self, windownName, windownUi):
        self.__configPath = "../config/ComponentList.json"
        self.__name_list = []
        self.__component_dict = {}
        self.__windownUi = windownUi
        self.__set_components(windownName)

    def __set_components(self, windownName):
        """
        设置窗体组件列表
        """
        _openDialog = openDialog()
        if not self.__windownUi:
            log.Show("控件对象为空，请核实", 40, "BaseComponent=>__set_components")
            _openDialog.showMessage("【baseComponent.py】=>set_components:控件对象为空，请核实")
            return
        if self.__component_dict:
            log.Show("已经存在控件字典", 40, "BaseComponent=>__set_components")
            _openDialog.showMessage("【baseComponent.py】=>set_components:已经存在控件字典")
            return
        if not self.__configPath:
            log.Show("窗口控件文件地址为空", 40, "【baseComponent.py】=>__set_components")
            _openDialog.showMessage("baseComponent=>set_components:窗口控件文件地址为空")
            return

        try:
            readConfigDatas = readConfig(self.__configPath)
            temp_list = readConfigDatas.get_forkey(windownName)
            if temp_list != "NULL":
                self.__name_list = temp_list
                for item in temp_list:
                    component_name = item.rstrip()
                    component = getattr(self.__windownUi, component_name)
                    if component:
                        self.__component_dict[component_name] = component
        except Exception as e:
            log.Show(e, 40, "BaseComponent=>__set_components")
            _openDialog.showMessage(f"【baseComponent.py】=>set_components:{e}")

    def get_windownUi(self):
        """
        获取窗体对象
        """
        return self.__windownUi

    def has_component_for_hasattr(self, component_name: str):
        """
        通过反射机制查找对象是否存在，存在返回 True
        """
        if hasattr(self.__windownUi, component_name):
            return True
        else:
            log.Show(f"查找不到该控件 {component_name}", 30, "【baseComponent.py】=> has_component_for_hasattr")
            return False

    def get_component_for_getattr(self, component_name):
        """
        通过反射机制获取对象，返回该对象
        """
        if self.has_component_for_hasattr(component_name):
            return getattr(self.__windownUi, component_name)
        else:
            return None

    def has_component(self, component_name: str):
        """
        通过组件名称，判断组件是否存在，存在返回True
        """
        if component_name in self.__component_dict.keys():
            return True
        else:
            log.Show(f"查找不到该控件 {component_name}", 30, "【baseComponent.py】=> has_component")
            return False

    def get_component(self, component_name):
        """
        通过组件名获取组件对象
        """
        if component_name in self.__component_dict.keys():
            return self.__component_dict[component_name]
        else:
            return None

    def get_namelist(self):
        """
        获取所有组件名称列表，仅提供复制样本
        """
        return copy.deepcopy(self.__name_list)

    def showWindown(self):
        """
        展示窗口
        """
        self.__windownUi.show()

    def showDialog(self):
        """
        展示提示窗\n
        """
        self.__windownUi.exec()

    def get_radioButton_value(self, qbojName: str) -> bool:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.isChecked()
            else:
                return False
        else:
            return self.get_component(qbojName).isChecked()

    def set_radioButton_value(self, qbojName: str, value: bool):
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setChecked(value)
        else:
            self.get_component(qbojName).setChecked(value)

    def get_checkBox_value(self, qbojName: str) -> bool:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.isChecked()
            else:
                return False
        else:
            return self.get_component(qbojName).isChecked()

    def set_checkBox_value(self, qbojName: str, value: bool):
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setChecked(value)
        else:
            self.get_component(qbojName).setChecked(value)

    def get_lineEdit_value(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.text()
            else:
                return ""
        else:
            return self.get_component(qbojName).text()

    def set_lineEdit_value(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setText(value)
        else:
            self.get_component(qbojName).setText(value)

    def get_plainEdit_value(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component
            else:
                return ""
        else:
            return self.get_component(qbojName).toPlainText()

    def set_plainEdit_appHtml(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.appendHtml(value)
        else:
            self.get_component(qbojName).appendHtml(value)

    def set_plainEdit_appText(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.appendPlainText(value)
        else:
            self.get_component(qbojName).appendPlainText(value)

    def set_plainEdit_text(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setPlainText(value)
        else:
            self.get_component(qbojName).setPlainText(value)

    def get_textEdit_value(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.toPlainText()
            else:
                return ""
        else:
            return self.get_component(qbojName).toPlainText()

    def set_textEdit_html(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setHtml(value)
        else:
            self.get_component(qbojName).setHtml(value)

    def set_textEdit_text(self, qbojName: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setText(value)
        else:
            self.get_component(qbojName).setText(value)

    def get_label_value(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.text()
            else:
                return ""
        else:
            return self.get_component(qbojName).text()

    def set_label_value(self, qbojName: str, style: str, value: str):
        if not value:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setText(f"<<<<<<<<< {value} >>>>>>>>>>")
                component.setStyleSheet(style)
        else:
            self.get_component(qbojName).setText(f"<<<<<<<<< {value} >>>>>>>>>>")
            self.get_component(qbojName).setStyleSheet(style)

    def get_comboBox_value(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.currentText()
            else:
                return ""
        else:
            return self.get_component(qbojName).currentText()

    def set_comboBox_insert(self, qbojName: str, index: int):
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.setCurrentIndex(index)
        else:
            self.get_component(qbojName).setCurrentIndex(index)

    def set_comboBox_Items(self, qbojName: str, datalist: list):
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.addItems(datalist)
        else:
            self.get_component(qbojName).addItems(datalist)

    def clear_comboBox(self, qbojName: str):
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.clear()
        else:
            self.get_component(qbojName).clear()

    def set_listWidget_list(self, qbojName: str, valueList: list):
        if not valueList:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.addItems(valueList)
        else:
            self.get_component(qbojName).addItems(valueList)

    def set_listWidget_item(self, qbojName: str, item: str):
        if not item:
            return

        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                component.addItem(item)
        else:
            self.get_component(qbojName).addItem(item)

    def get_listWidget_currentItem(self, qbojName: str) -> str:
        if not self.has_component(qbojName):
            component = self.get_component_for_getattr(qbojName)
            if component:
                return component.currentItem()
            else:
                return ""
        else:
            return self.get_component(qbojName).currentItem()
